package com.yunshu.yunshuapp.kuaixun;

import android.app.Activity;
import android.content.SharedPreferences;

import com.fasterxml.jackson.annotation.JsonIgnoreProperties;
import com.fasterxml.jackson.annotation.JsonProperty;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.yunshu.yunshuapp.R;
import com.yunshu.yunshuapp.network.YSCallback;
import com.yunshu.yunshuapp.network.YSRequest;

import java.io.IOException;
import java.util.ArrayList;
import java.util.Iterator;

/**
 * Created by zhouwenjie on 2016/12/22.
 */
@JsonIgnoreProperties(ignoreUnknown = true)
public class KuaixunListModel {

    @JsonIgnoreProperties
    private boolean isBanner;
    @JsonIgnoreProperties
    private boolean isRead;

    @JsonProperty("id")
    private String modelId;

    @JsonProperty("content")
    private String title;

    @JsonProperty("praiseNum")
    private String favourateCount;
    @JsonProperty("pushtime")
    private String date;


    public String getModelId() {
        return modelId;
    }

    public void setModelId(String modelId) {
        this.modelId = modelId;
    }

    public String getTitle() {
        return title;
    }

    public void setTitle(String title) {
        this.title = title;
    }

    public String getFavourateCount() {
        return favourateCount;
    }

    public void setFavourateCount(String favourateCount) {
        this.favourateCount = favourateCount;
    }

    public String getDate() {
        return date;
    }

    public void setDate(String date) {
        this.date = date;
    }

    public boolean isBanner() {
        return isBanner;
    }

    public void setBanner(Boolean banner) {
        isBanner = banner;
    }

    public void setRead(Boolean newValue) {
        isRead = newValue;
        if (newValue){
            readList.add(this.modelId);
        }
    }

    public boolean isRead(){return  isRead;}

    public String serialize() {
        String kuaixunStr = null ;
        ObjectMapper mapper = new ObjectMapper();
        try {
            kuaixunStr = mapper.writeValueAsString(this);
        } catch (JsonProcessingException e) {
            e.printStackTrace();
        }

        return kuaixunStr;
    }

    public void updateIsRead(){
        if (readList == null || readList.size() == 0) this.setRead(false);
        for (String id : readList){
            if ( id.equalsIgnoreCase(this.modelId)) {
                this.isRead = true;
                return;
            }
        }
        this.setRead(false);
    }

    static ArrayList<String> readList;
    private static final int MODE_PRIVATE = 0;

    public static void readListFromPersistence(Activity activity){
        //获取SharedPreferences对象
        SharedPreferences sp = activity.getSharedPreferences("SP", MODE_PRIVATE);
        String readListStr = sp.getString("ssssReadList", "");
        String[] result = readListStr.split(",");
        readList = new ArrayList<String>();
        for (String id :result)
            readList.add(id);
    }

    public static void persistentReadList(Activity activity){
        StringBuffer result = new StringBuffer();
        if (readList != null){
            for (String id : readList) {
                result.append(id);
                result.append(",");
            }
        }
        SharedPreferences sp = activity.getSharedPreferences("SP", MODE_PRIVATE);
        //存入数据
        SharedPreferences.Editor editor = sp.edit();
        editor.putString("ssssReadList", result.toString());
        editor.commit();
    }

    static ArrayList<String> loveList;
    public Boolean isLoved(Activity activity){
        if (loveList == null) {
            //获取SharedPreferences对象
            SharedPreferences sp = activity.getSharedPreferences("SP", MODE_PRIVATE);
            String readListStr = sp.getString("ssssLoveList", "");
            String[] result = readListStr.split(",");
            loveList = new ArrayList<String>();
            for (String id : result)
                loveList.add(id);
        }
        for (String id : loveList){
            if (id.equalsIgnoreCase(this.modelId))
                return true;
        }
        return false;
    }

    public void setLoved(Boolean isLoved,Activity activity){
        if (loveList == null) {
            //获取SharedPreferences对象
            SharedPreferences sp = activity.getSharedPreferences("SP", MODE_PRIVATE);
            String readListStr = sp.getString("ssssLoveList", "");
            String[] result = readListStr.split(",");
            loveList = new ArrayList<String>();
            for (String id : result)
                loveList.add(id);
        }
        if (isLoved) {
            loveList.add(this.modelId);
        }else {
            int deleteIndex = -1;
            for (int i = 0 ;i <loveList.size(); i++){
                String id = loveList.get(i);
                if (id.equalsIgnoreCase(this.modelId))
                    deleteIndex = i;
            }
            if (deleteIndex >=0)
                loveList.remove(deleteIndex);
        }

        StringBuffer result = new StringBuffer();
        if (loveList != null){
            for (String id : loveList) {
                result.append(id);
                result.append(",");
            }
        }
        SharedPreferences sp = activity.getSharedPreferences("SP", MODE_PRIVATE);
        //存入数据
        SharedPreferences.Editor editor = sp.edit();
        editor.putString("ssssLoveList", result.toString());
        editor.commit();

    }
}
